package com.wonderli.wondertweet;

import java.util.ArrayList;
import java.util.List;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Timeline extends Activity{
    public final String TAG = getClass().getName();      
    private Twitter twitter;
    private ConfigurationBuilder cb;
    private SharedPreferences settings;
    private String accesstoken;
    private String secrettoken;
    private ListView listview;
    private List<Status> statuses;
    private List<String> list; 
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.timeline);	
		listview = (ListView) findViewById(R.id.listview);
		settings = getSharedPreferences(Constants.SETTINGS,0);				
		accesstoken = settings.getString("accesstoken", "default");
		secrettoken = settings.getString("secrettoken", "default");
		System.out.println(accesstoken);
		System.out.println(secrettoken);
		if(accesstoken.equalsIgnoreCase("default")) {
			Intent intent = new Intent(Timeline.this, Prefs.class);
			Toast.makeText(Timeline.this, "You need to Oauth this application", 10000).show();
			startActivity(intent);					
		} else {
			cb = new ConfigurationBuilder();					
			cb.setDebugEnabled(true).setOAuthConsumerKey(Constants.CONSUMER_KEY).setOAuthConsumerSecret(Constants.CONSUMER_SECRET).setOAuthAccessToken(accesstoken).setOAuthAccessTokenSecret(secrettoken);					
			twitter = new TwitterFactory(cb.build()).getInstance();
			try {			    
				statuses = twitter.getFriendsTimeline();
			    list = new ArrayList<String>();
			    System.out.println("Showing friends timeline.");
			    StringBuilder stringbuilder = new StringBuilder();			    
			    for (Status status : statuses) {
			        //System.out.println(status.getUser().getName() + ":" +
			        //                   status.getText());
			    	String str = status.getUser().getName() + ":" + status.getText();
			        //String str = stringbuilder.append(status.getUser().getName()).append(":").append(status.getText()).toString(); 
			        System.out.println(str);    
			        list.add(str);			        
			    }			    
				listview.setAdapter(new ArrayAdapter<String>(Timeline.this, R.layout.onestatus, list));
				listview.setOnItemClickListener(new OnItemClickListener(){

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						
					}
					
				});

			} catch (TwitterException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
				
		}
						
	}	
}




